package com.aspose.cells.a.d;

import com.aspose.cells.Encoding;
import com.aspose.cells.PdfSecurityOptions;

public class zet extends zef {
   private PdfSecurityOptions g;
   public zdk a = null;
   static byte[] b = new byte[]{40, -65, 78, 94, 78, 117, -118, 65, 100, 0, 78, 86, -1, -6, 1, 8, 46, 46, 0, -74, -48, 104, 62, -128, 47, 12, -87, -2, 100, 83, 105, 122};
   private byte[] h;
   private com.aspose.cells.a.c.zr i = new com.aspose.cells.a.c.zr();
   private byte[] j = new byte[256];
   private byte[] k = new byte[32];
   private byte[] l = new byte[32];
   private byte[] m;
   private int n;
   public int c;

   public zet(zdk var1, PdfSecurityOptions var2) throws Exception {
      super(var1.k());
      this.a = var1;
      var1.a(this);
      this.g = var2;
   }

   public String b() {
      return this.g.getUserPassword();
   }

   public void a(String var1) {
      this.g.setUserPassword(var1);
   }

   public String c() {
      return this.g.getOwnerPassword();
   }

   public void b(String var1) {
      this.g.setOwnerPassword(var1);
   }

   private int e() {
      int var1 = 0;
      if (this.g.getAnnotationsPermission()) {
         var1 |= 32;
      }

      if (this.g.getAssembleDocumentPermission()) {
         var1 |= 1024;
      }

      if (this.g.getExtractContentPermission()) {
         var1 |= 16;
      }

      if (this.g.getExtractContentPermissionObsolete()) {
         var1 |= 16;
      }

      if (this.g.getFillFormsPermission()) {
         var1 |= 256;
      }

      if (this.g.getFullQualityPrintPermission()) {
         var1 |= 2048;
      }

      if (this.g.getModifyDocumentPermission()) {
         var1 |= 8;
      }

      if (this.g.getPrintPermission()) {
         var1 |= 4;
      }

      return var1;
   }

   public void a(zeg var1) throws Exception {
      var1.a(this);
      var1.g();
      var1.a("/Filter", "/Standard");
      var1.a("/Length", "128");
      var1.a("/O <");
      var1.a(this.k);
      var1.a(">");
      var1.a("/U <");
      var1.a(this.l);
      var1.a(">");
      var1.a("/P", this.c);
      var1.a("/V", "2");
      var1.a("/R", "3");
      var1.h();
      var1.b();
   }

   public void a(com.aspose.cells.b.a.d.zh var1) throws Exception {
      if (var1 != null && var1.h() != 0L) {
         this.f();
         byte[] var2 = var1.m();
         this.b(var2, 0, (int)var1.h());
      }

   }

   public byte[] c(String var1) throws Exception {
      byte[] var2 = Encoding.getBigEndianUnicode().a(var1);
      byte[] var4 = new byte[2 + var2.length];
      System.arraycopy(var2, 0, var4, 2, var2.length);
      var4[0] = -2;
      var4[1] = -1;
      if (var2 != null && var2.length != 0) {
         this.f();
         this.b(var4, 0, var4.length);
      }

      return var4;
   }

   public byte[] d(String var1) throws Exception {
      byte[] var2 = com.aspose.cells.b.a.g.za.getASCII().a(var1);
      if (var2 != null && var2.length != 0) {
         this.f();
         this.b(var2, 0, var2.length);
      }

      return var2;
   }

   public void d() throws Exception {
      this.c = this.e();
      boolean var1 = true;
      if (this.b() == null || this.b().length() == 0) {
         this.a("");
      }

      if (this.c() == null || this.c().length() == 0) {
         this.b(this.b());
      }

      this.c |= (int)(var1 ? 4294963392L : 4294967232L);
      this.c &= -4;

      assert this.c().length() > 0 : "Empty owner password.";

      byte[] var2 = this.e(this.b());
      byte[] var3 = this.e(this.c());
      this.i.a();
      this.k = this.a(var2, var3, var1);
      byte[] var4 = this.a.l();
      this.a(var4, this.b(), this.k, this.c, var1);
   }

   private byte[] a(byte[] var1, byte[] var2, boolean var3) {
      byte[] var4 = new byte[32];
      byte[] var5 = this.i.a(var2);
      if (var3) {
         byte[] var6 = new byte[16];

         int var7;
         for(var7 = 0; var7 < 50; ++var7) {
            var5 = this.i.a(var5);
         }

         System.arraycopy(var1, 0, var4, 0, 32);

         for(var7 = 0; var7 < 20; ++var7) {
            for(int var8 = 0; var8 < var6.length; ++var8) {
               var6[var8] = (byte)(var5[var8] & 255 ^ var7);
            }

            this.b(var6);
            this.c(var4);
         }
      } else {
         this.a(var5, 0, 5);
         this.a(var1, var4);
      }

      return var4;
   }

   public void a(zef var1) {
      byte[] var2 = new byte[5];
      this.i.a();
      var2[0] = (byte)var1.m();
      var2[1] = (byte)(var1.m() >> 8);
      var2[2] = (byte)(var1.m() >> 16);
      var2[3] = 0;
      var2[4] = 0;
      this.i.a(this.h, 0, this.h.length, this.h, 0);
      this.i.a(var2, 0, var2.length);
      this.m = this.i.b();
      this.i.a();
      this.n = this.h.length + 5;
      if (this.n > 16) {
         this.n = 16;
      }

   }

   private void a(byte[] var1, String var2, byte[] var3, int var4, boolean var5) throws Exception {
      this.a(var1, this.e(var2), var3, var4, var5);
      this.a(var1);
   }

   private void a(byte[] var1, byte[] var2, byte[] var3, int var4, boolean var5) {
      this.k = var3;
      this.h = new byte[var5 ? 16 : 5];
      this.i.a();
      this.i.a(var2, 0, var2.length, var2, 0);
      this.i.a(var3, 0, var3.length, var3, 0);
      byte[] var6 = new byte[]{(byte)var4, (byte)(var4 >> 8), (byte)(var4 >> 16), (byte)(var4 >> 24)};
      this.i.a(var6, 0, 4, var6, 0);
      this.i.a(var1, 0, var1.length, var1, 0);
      this.i.a(var6, 0, 0);
      byte[] var7 = this.i.b();
      this.i.a();
      if (this.h.length == 16) {
         for(int var8 = 0; var8 < 50; ++var8) {
            var7 = this.i.a(var7);
            this.i.a();
         }
      }

      System.arraycopy(var7, 0, this.h, 0, this.h.length);
   }

   private void a(byte[] var1) {
      if (this.h.length == 16) {
         this.i.a(b, 0, b.length, b, 0);
         this.i.a(var1, 0, var1.length);
         byte[] var2 = this.i.b();
         this.i.a();
         System.arraycopy(var2, 0, this.l, 0, 16);

         int var3;
         for(var3 = 16; var3 < 32; ++var3) {
            this.l[var3] = 0;
         }

         for(var3 = 0; var3 < 20; ++var3) {
            for(int var4 = 0; var4 < this.h.length; ++var4) {
               var2[var4] = (byte)(this.h[var4] & 255 ^ var3);
            }

            this.a(var2, 0, this.h.length);
            this.b(this.l, 0, 16);
         }
      } else {
         this.b(this.h);
         this.a(b, this.l);
      }

   }

   private byte[] e(String var1) throws Exception {
      byte[] var2 = new byte[32];
      if (var1 == null) {
         System.arraycopy(b, 0, var2, 0, 32);
      } else {
         int var3 = var1.length();
         System.arraycopy(com.aspose.cells.b.a.g.za.getASCII().a(var1), 0, var2, 0, Math.min(var3, 32));
         if (var3 < 32) {
            System.arraycopy(b, 0, var2, var3, 32 - var3);
         }
      }

      return var2;
   }

   private void f() {
      this.a(this.m, 0, this.n);
   }

   private void b(byte[] var1) {
      this.a(var1, 0, var1.length);
   }

   private void a(byte[] var1, int var2, int var3) {
      int var4 = 0;
      int var5 = 0;

      for(int var6 = 0; var6 < 256; ++var6) {
         this.j[var6] = (byte)var6;
      }

      for(int var7 = 0; var7 < 256; ++var7) {
         var5 = (var1[var4 + var2] & 255) + (this.j[var7] & 255) + var5 & 255;
         byte var8 = this.j[var7];
         this.j[var7] = this.j[var5];
         this.j[var5] = var8;
         var4 = (var4 + 1) % var3;
      }

   }

   private void c(byte[] var1) {
      this.a(var1, 0, var1.length, var1);
   }

   private void b(byte[] var1, int var2, int var3) {
      this.a(var1, var2, var3, var1);
   }

   private void a(byte[] var1, byte[] var2) {
      this.a(var1, 0, var1.length, var2);
   }

   private void a(byte[] var1, int var2, int var3, byte[] var4) {
      var3 += var2;
      int var5 = 0;
      int var6 = 0;

      for(int var8 = var2; var8 < var3; ++var8) {
         var5 = var5 + 1 & 255;
         var6 = (this.j[var5] & 255) + var6 & 255;
         byte var7 = this.j[var5];
         this.j[var5] = this.j[var6];
         this.j[var6] = var7;
         var4[var8] = (byte)(var1[var8] & 255 ^ this.j[(this.j[var5] & 255) + (this.j[var6] & 255) & 255] & 255);
      }

   }
}
